package com.gill.exp.common.exception.handle;

import com.gill.exp.common.HttpResult;
import com.gill.exp.common.enums.ResultCodeEnum;
import com.gill.exp.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author: Sawyer
 * @Description: 统一异常处理及返回对象封装
 * @Date: Created in 上午11:17 17/8/11
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler{

    @Autowired
    HttpServletRequest httpServletRequest;

    /**
     * 不支持的请求类型
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    private HttpResult httpRequestMethodNotSupported(HttpRequestMethodNotSupportedException exception) {
        logErrorRequest(exception);
        return HttpResult.fail(ResultCodeEnum.UNSUPPORTED_METHOD);
    }

    /**
     * 缺少参数
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    private HttpResult missingServletRequestParameter(MissingServletRequestParameterException exception) {
        logErrorRequest(exception);
        return HttpResult.fail(ResultCodeEnum.MISSING_PARAM);
    }

    /**
     * 参数格式有误
     */
    @ExceptionHandler({MethodArgumentTypeMismatchException.class, HttpMessageNotReadableException.class})
    private HttpResult typeMismatch(Exception exception) {
        logErrorRequest(exception);
        return HttpResult.fail(ResultCodeEnum.MISTYPE_PARAM);
    }

    /**
     * 参数未通过@Valid验证异常，
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    private HttpResult methodArgumentNotValid(MethodArgumentNotValidException exception) {
        logErrorRequest(exception);
        return HttpResult.fail(ResultCodeEnum.INVALID_PARAM);
    }

    /**
     * 业务层异常
     */
    @ExceptionHandler(BusinessException.class)
    private HttpResult serviceExceptionHandler(BusinessException exception) {
        logErrorRequest(exception);
        return HttpResult.fail(ResultCodeEnum.S_SYS_UNKNOWN.getCode(), exception.getMessage());
    }

    /**
     * 其他系统异常
     */
    @ExceptionHandler({HttpClientErrorException.class, IOException.class, Exception.class})
    private HttpResult commonExceptionHandler(Exception exception) {
        logErrorRequest(exception);
        return HttpResult.fail(ResultCodeEnum.S_SYS_ERROR.getCode(),exception.getMessage());
    }

    /**
     * 异常日志记录
     */
    private void logErrorRequest(Exception e) {
        log.error("报错API URL:{}", httpServletRequest.getRequestURL().toString());
        log.error("异常:{}", e.getMessage());
    }
}